﻿using Engine;
using Engine.Graphics;
using Game;
using System;
namespace Mekiasm
{
    public class ImageWidget : CanvasWidget
    {
        public Vector2 textcora = new Vector2(1, 1);
        public Action Clicked;
        public Texture2D Texture_;
        public Texture2D Texture
        {
            get
            {
                return Texture_;
            }
            set
            {
                Texture_ = value;
            }
        }
        public Vector2 startPoint = Vector2.Zero;
        public Vector2 endPoint = Vector2.One;
        public bool AlignRightBottom;
        public Color Color
        {
            get; set;
        }
        public ImageWidget(bool arb = false)
        {
            AlignRightBottom = arb;
            IsHitTestVisible = false;
            IsDrawRequired = true;
            HorizontalAlignment = WidgetAlignment.Near;
            VerticalAlignment = WidgetAlignment.Near;
            Color = Color.White;
        }
        public void setTexcoord(Vector2 start, Vector2 end)
        {
            startPoint = start;
            endPoint = end;
        }

        public override void Draw(DrawContext dc)
        {
            if (Texture != null)
            {
                TexturedBatch2D texturedBatch2D = dc.PrimitivesRenderer2D.TexturedBatch(Texture, useAlphaTest: false, 0, DepthStencilState.None, null, BlendState.NonPremultiplied, SamplerState.PointWrap);
                int count = texturedBatch2D.TriangleVertices.Count;
                texturedBatch2D.QueueQuad(Vector2.Zero, Size, 0f, startPoint, endPoint, Color);
                texturedBatch2D.TransformTriangles(GlobalTransform, count);
            }
        }
        public override void Update()
        {
            if (Input.Click.HasValue && HitTest(Input.Click.Value.End) && HitTest(Input.Click.Value.Start)) Clicked?.Invoke();
        }
    }
}
